import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

	
public class ProcessList
{
	  private BlockingQueue<Process> fila;
	  private String myName = "No name";
	  private String tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
	  
	  public ProcessList(String name)
	  { 
		  myName=name ;
		  fila = new LinkedBlockingQueue<Process>();
	  }
	  
	  public Process getFront()
	  { 
		  if(fila.isEmpty())
		  {
			  System.out.println(tabs+"Fila vazia");
			  return null;  
		  }
		  else
			  return fila.element();
	  }
	  
	  public Boolean isEmpty()
	  {
		  if(fila.isEmpty())
			  return true;
		  else
			  return false;
	  }
	  
	  
	  public void print()
	  {
		  System.out.print(tabs+myName+": ");
		
		  for (Process n:fila)
			  System.out.print(n.PID+" ");
				  
		  System.out.println("");
	  }
			
	  public Process popFront() 
	  {
		  Process p = null;
		  
		  if(fila.isEmpty())
		  {
			  System.out.println(tabs+"Fila vazia");
			  return null;  
		  }
		  else
		  {
			  //IF 2 threads (the 2 CPUs) try to acess the blocking queue
			  try
				{
					p = fila.take();
				} catch (InterruptedException e) { return null; }
			  
			  
			  
			  
		  }
	      
		  return p;
	   }
	  
	  public void pushBack(Process p) 
	  { 
			if(!fila.add(p))
			{
				System.out.println(tabs+"FILA "+ myName + " Erro ao adicionar processo atras da fila");
				System.exit(1);
			}
		  
	   }
}